package org.sgroup.imenu.util;

import java.io.StringWriter;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

import org.sgroup.imenu.bo.Category;
import org.sgroup.imenu.bo.Item;
import org.xmlpull.v1.XmlSerializer;
import android.util.Xml;

public class IXmlMapper {
	public static <T> String MapToXML(Class<T> c, T _object, String version) {
		String ClassName = c.getSimpleName();
		XmlSerializer serializer = Xml.newSerializer();
		StringWriter writer = new StringWriter();
		try {
			serializer.setOutput(writer);
			serializer.startDocument("UTF-8", true);
			serializer.startTag("", "Data");
			serializer.attribute("", "version", version);
			serializer.startTag("", ClassName);
			for (Field f : c.getDeclaredFields()) {
				String name;
				Object value;

				f.setAccessible(true);
				name = f.getName();
				try {
					value = f.get(_object);
					if (value == null)
						value = " ";
					serializer.attribute("", name, String.valueOf(value));
				} catch (IllegalArgumentException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IllegalAccessException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			serializer.endTag("", ClassName);
			serializer.endTag("", "Data");
			serializer.endDocument();
			return writer.toString();
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	public static <T> String MapToXML(Class<T> c, List<T> _object) {
		String ClassName = c.getSimpleName();

		XmlSerializer serializer = Xml.newSerializer();
		StringWriter writer = new StringWriter();
		try {
			serializer.setOutput(writer);
			serializer.startDocument("UTF-8", true);
			serializer.startTag("", "Data");

			for (T obj : _object) {
				serializer.startTag("", ClassName);
				for (Field f : c.getDeclaredFields()) {
					String name;
					Object value;
					f.setAccessible(true);
					name = f.getName();
					try {
						value = f.get(obj);
						if (value == null)
							value = " ";
						serializer.attribute("", name, String.valueOf(value));
					} catch (IllegalArgumentException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (IllegalAccessException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
				serializer.endTag("", ClassName);
			}
			serializer.endTag("", "Data");
			serializer.endDocument();

			return writer.toString();
		} catch (Exception e) {
			throw new RuntimeException(e);
		}

	}

	public static <T> XmlSerializer MapToXmlSerializer(Class<T> c,
			List<T> _object, XmlSerializer ser) {
		String ClassName = c.getSimpleName();
		try {
			for (T obj : _object) {
				ser.startTag("", ClassName);
				List<Item> item = null;
				for (Field f : c.getDeclaredFields()) {
					String name;
					Object value;
					f.setAccessible(true);
					name = f.getName();
					try {
						value = f.get(obj);
						if (name.toString().equalsIgnoreCase("items")
								&& value != null) {
							item = (ArrayList<Item>) value;
							value = " ";
						}
						if (value == null)
							value = " ";
						ser.attribute("", name, String.valueOf(value));
					} catch (IllegalArgumentException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (IllegalAccessException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
				if (item != null) {
					XmlSerializer temp = Xml.newSerializer();
					ser = MapToXmlSerializer(Item.class, item, ser);
				}
				ser.endTag("", ClassName);
			}
			// serializer.endDocument();

			return ser;
		} catch (Exception e) {
			throw new RuntimeException(e);
		}

	}

	public static <T> XmlSerializer MapItemToXml(Class<T> c, List<T> _object,
			XmlSerializer ser) {
		String ClassName = c.getSimpleName();
		// StringWriter writer = new StringWriter();
		try {
			// serializer.setOutput(writer);
			for (T obj : _object) {
				ser.startTag("", ClassName);

				for (Field f : c.getDeclaredFields()) {
					String name;
					Object value;
					f.setAccessible(true);
					name = f.getName();
					try {
						value = f.get(obj);
						if (value == null)
							value = " ";
						ser.attribute("", name, String.valueOf(value));
					} catch (IllegalArgumentException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (IllegalAccessException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
				ser.endTag("", ClassName);
			}
			// serializer.endDocument();

			return ser;
		} catch (Exception e) {
			throw new RuntimeException(e);
		}

	}

	public static <T> String MapToXML(Class<T> c, List<T> _object,
			boolean isChirl, String version) {
		String ClassName = c.getSimpleName();
		XmlSerializer serializer = Xml.newSerializer();
		StringWriter writer = new StringWriter();
		try {
			serializer.setOutput(writer);
			serializer.startDocument("UTF-8", true);
			serializer.startTag("", "Data");
			serializer.attribute("", "version", version);
			for (T obj : _object) {
				serializer.startTag("", ClassName);
				List<Category> chirlCat = null;
				for (Field f : c.getDeclaredFields()) {
					String name;
					Object value;
					f.setAccessible(true);
					name = f.getName();
					try {
						value = f.get(obj);
						if (name.toString().equalsIgnoreCase("childs")
								&& value != null) {
							chirlCat = (ArrayList<Category>) value;
							value = " ";
						}
						if (value == null)
							value = " ";
						serializer.attribute("", name, String.valueOf(value));

					} catch (IllegalArgumentException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (IllegalAccessException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
				if (chirlCat != null) {
					XmlSerializer temp = Xml.newSerializer();
					serializer = MapToXmlSerializer(Category.class, chirlCat,
							serializer);
				}
				serializer.endTag("", ClassName);
			}
			serializer.endTag("", "Data");
			serializer.endDocument();

			return writer.toString();
		} catch (Exception e) {
			throw new RuntimeException(e);
		}

	}
}
